package com.study.service.impl;

import com.study.entity.*;
import com.study.mapper.BlogUserMapper;
import com.study.mapper.FriendMapper;
import com.study.mapper.TFriendMessageMapper;
import com.study.mapper.ext.BlogUserExtMapper;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private BlogUserMapper blogUserMapper;

    @Autowired
    private BlogUserExtMapper blogUserExtMapper;

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private TFriendMessageMapper friendMessageMapper;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public Integer userSign(BlogUser user) {
        Date date = new Date();
        user.setAddTime(date);
        user.setModTime(date);
        user.setIsDel(0);
        return blogUserMapper.insertSelective(user);
    }

    @Override
    public List<BlogUser> queryUserByUsernameAndPassword(BlogUser user) {
        BlogUserExample userExample = new BlogUserExample();
        userExample.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword());
        return blogUserMapper.selectByExample(userExample);
    }

    @Override
    public List<BlogUser> getUserByUsername(String username) {
        BlogUserExample userExample = new BlogUserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        return blogUserMapper.selectByExample(userExample);
    }

    @Override
    public List<BlogUser> getUserByMobile(String mobile) {
        BlogUserExample userExample = new BlogUserExample();
        userExample.createCriteria().andMobileEqualTo(mobile);
        return blogUserMapper.selectByExample(userExample);
    }

    @Override
    public List<BlogUser> getFriendList(BlogUser blogUser) {
        //查询当前用户id
        BlogUserExample userExample = new BlogUserExample();
        userExample.createCriteria().andUsernameEqualTo(blogUser.getUsername());
        List<BlogUser> users = blogUserMapper.selectByExample(userExample);

        FriendExample friendExample = new FriendExample();
        FriendExample.Criteria criteria = friendExample.createCriteria();
        criteria.andFromUserIdEqualTo(users.get(0).getId());
        FriendExample.Criteria criteria2 = friendExample.createCriteria();
        criteria2.andToUserIdEqualTo(users.get(0).getId());
        friendExample.or(criteria2);

        List<Friend> friends = friendMapper.selectByExample(friendExample);
        if (CollectionUtils.isEmpty(friends)){
            return null;
        }else {
            List<Integer> list = new ArrayList<>();
            for (Friend friend : friends) {
                if (friend.getFromUserId().equals(users.get(0).getId())){
                    list.add(friend.getToUserId());
                }else {
                    list.add(friend.getFromUserId());
                }
            }
            BlogUserExample blogUserExample = new BlogUserExample();
            blogUserExample.createCriteria().andIdIn(list);
            return blogUserMapper.selectByExample(blogUserExample);
        }
    }

    @Override
    public List<BlogUser> getUserLikeUsername(BlogUser blogUser,BlogUser loginUser) {
        List<BlogUser> blogUsers = blogUserExtMapper.getUserLikeUsername(blogUser);
        for (BlogUser user : blogUsers) {
            if (user.getId() == loginUser.getId()){
                blogUsers.remove(user);
                break;
            }
        }
        return blogUsers;
    }

    @Override
    public List<TFriendMessage> getFriendMessage(BlogUser blogUser) {
        TFriendMessageExample friendMessageExample = new TFriendMessageExample();
        friendMessageExample.createCriteria().andToUserIdEqualTo(blogUser.getId()).andIsDealEqualTo(0);
        return friendMessageMapper.selectByExample(friendMessageExample);
    }

    @Override
    public Integer addFriendMessage(BlogUser blogUser,BlogUser userInfo) {
        //获取登录用户信息
        TFriendMessage tFriendMessage = new TFriendMessage();
        tFriendMessage.setFromUserId(userInfo.getId());
        tFriendMessage.setToUserId(blogUser.getId());
        tFriendMessage.setIsDeal(0);
        tFriendMessage.setIsDel(0);
        return friendMessageMapper.insertSelective(tFriendMessage);
    }

    @Override
    public List<TFriendMessage> getUserName(List<TFriendMessage> messageList) {
        for (TFriendMessage tFriendMessage : messageList) {
            BlogUserExample userExample = new BlogUserExample();
            userExample.createCriteria().andIdEqualTo(tFriendMessage.getFromUserId());
            List<BlogUser> fromList = blogUserMapper.selectByExample(userExample);
            if (!CollectionUtils.isEmpty(fromList)){
                tFriendMessage.setFromUserName(fromList.get(0).getUsername());
            }
            BlogUserExample userExample1 = new BlogUserExample();
            userExample1.createCriteria().andIdEqualTo(tFriendMessage.getToUserId());
            List<BlogUser> toList = blogUserMapper.selectByExample(userExample);
            if (!CollectionUtils.isEmpty(toList)){
                tFriendMessage.setToUserName(toList.get(0).getUsername());
            }
        }
        return messageList;
    }

    @Override
    public boolean agree(TFriendMessage friendMessage, BlogUser userInfo) {
        //更新信息表的信息状态
        TFriendMessageExample friendMessageExample = new TFriendMessageExample();
        friendMessageExample.createCriteria().andIdEqualTo(friendMessage.getId());
        List<TFriendMessage> list = friendMessageMapper.selectByExample(friendMessageExample);
        TFriendMessage tFriendMessage = new TFriendMessage();
        tFriendMessage.setIsDeal(1);
        TFriendMessageExample updateExample = new TFriendMessageExample();
        updateExample.createCriteria().andIdEqualTo(friendMessage.getId());
        int i = friendMessageMapper.updateByExampleSelective(tFriendMessage, updateExample);
        //在好友表添加关系
        Friend friend = new Friend();
        friend.setFromUserId(list.get(0).getFromUserId());
        friend.setToUserId(list.get(0).getToUserId());
        friend.setIsDel(0);
        friend.setAddTime(new Date());
        int insertSelective = friendMapper.insertSelective(friend);
        if (i>0 && insertSelective>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean disAgree(TFriendMessage friendMessage, BlogUser userInfo) {
        //更新信息表的信息状态
        TFriendMessageExample friendMessageExample = new TFriendMessageExample();
        friendMessageExample.createCriteria().andIdEqualTo(friendMessage.getId());
        TFriendMessage tFriendMessage = new TFriendMessage();
        tFriendMessage.setIsDeal(1);
        int i = friendMessageMapper.updateByExampleSelective(tFriendMessage, friendMessageExample);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

}
